package cn.codingjc.provider.config;

import org.springframework.boot.autoconfigure.security.servlet.WebSecurityEnablerConfiguration;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfiguration;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
import org.springframework.security.crypto.factory.PasswordEncoderFactories;
import org.springframework.security.crypto.password.PasswordEncoder;

/**
 * @author shenjicheng
 * @date 2022/7/3 9:43 PM
 */

@Configuration
public class SwaggerSecurityConfig extends WebSecurityConfigurerAdapter {

    private static final String DEFAULT_PASSWORD = "{bcrypt}$2a$10$OGurywosq.UNIzSIu1gVjuYREPOdsUvTryDVmAOOI6E/dBkBSyMgK";

    @Bean
    public PasswordEncoder getPasswordEncoder(){
        return PasswordEncoderFactories.createDelegatingPasswordEncoder();
    }

    @Override
    protected void configure(AuthenticationManagerBuilder auth) throws Exception {
        auth.inMemoryAuthentication()
                .withUser("swagger")
                .password(DEFAULT_PASSWORD)
                .roles("ADMIN", "USER");
    }

    @Override
    protected void configure(HttpSecurity http) throws Exception {
        http.authorizeRequests()
                .antMatchers("/swagger-ui/**", "/v2/api-docs").hasRole("ADMIN")
                .and()
                .httpBasic()
                .and()
                .formLogin()
                .and()
                .csrf()
                .disable();
    }
}
